<?php

namespace lxs\tiktok;

use Illuminate\Support\Facades\Log;
use lxs\aliyun\OssStorage;
use lxs\tools\Tools;
use OSS\Http\RequestCore_Exception;

class DyQrcode extends BaseController
{

    /**
     * 生成抖音小程序小程序码
     */
    public function getMiniCode($path, $scene, $width = 280, $appid, $secret)
    {
        $accessToken = $this->getAccessToken($appid, $secret);
        if (!$accessToken) {
            return false;
        }
        $url = 'https://open.douyin.com/api/apps/v1/qrcode/create';
        $qrcode = array(
            'appid'         => $appid,
            'app_name'       => 'douyin',
            'is_circle_code'=> true,
            'path'          => urlencode($path.$scene), //二维码跳转路径（要已发布小程序）
            'set_icon'      => true,
            'width'         => $width,
        );
        $post_data = json_encode($qrcode);
        $header = array(
            'content-type: application/json',
            'access-token: '.$accessToken
        );
        $data = Tools::httpRequest($url, $post_data, 'POST', $header);
        $result = json_decode($data, true);
        if ($result['err_no'] == 0) {
            $res = (new OssStorage())->base64ToOss($result['data']['img'], 'qrcode', 'tiktok');
            if ($res['code'] == 200) {
                return $res['oss_url'];
            }
        }
        return false;
    }
}
